<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Formatters - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.log.formatters.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.log.formatters.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.log.writers.html">Writers</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.log.html">Zend_Log</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.log.filters.html">Filters</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.log.formatters" class="section"><div class="info"><h1 class="title">Formatters</h1></div>
    

    <p class="para">
        A Formatter is an object that is responsible for taking an <span class="property">event</span> array
        describing a log event and outputting a string with a formatted log line.
    </p>

    <p class="para">
        Some Writers are not line-oriented and cannot use a Formatter. An example is the
        Database Writer, which inserts the event items directly into database columns. For
        Writers that cannot support a Formatter, an exception is thrown if you attempt to
        set a Formatter.
    </p>

    <div class="section" id="zend.log.formatters.simple"><div class="info"><h1 class="title">Simple Formatting</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Log_Formatter_Simple</span> is the default formatter. It is
            configured automatically when you specify no formatter. The default configuration is
            equivalent to the following:
        </p>

        <pre class="programlisting brush: php">
$format = &#039;%timestamp% %priorityName% (%priority%): %message%&#039; . PHP_EOL;
$formatter = new Zend_Log_Formatter_Simple($format);
</pre>


        <p class="para">
            A formatter is set on an individual Writer object using the Writer&#039;s
             <span class="methodname">setFormatter()</span> method:
        </p>

        <pre class="programlisting brush: php">
$writer = new Zend_Log_Writer_Stream(&#039;php://output&#039;);
$formatter = new Zend_Log_Formatter_Simple(&#039;hello %message%&#039; . PHP_EOL);
$writer-&gt;setFormatter($formatter);

$logger = new Zend_Log();
$logger-&gt;addWriter($writer);

$logger-&gt;info(&#039;there&#039;);

// outputs &quot;hello there&quot;
</pre>


        <p class="para">
            The constructor of <span class="classname">Zend_Log_Formatter_Simple</span> accepts a single
            parameter: the format string. This string contains keys surrounded by
            percent signs (e.g. <strong class="command">%message%</strong>). The format string may
            contain any key from the event data array.
            You can retrieve the default keys by using the DEFAULT_FORMAT constant from
            <span class="classname">Zend_Log_Formatter_Simple</span>.
        </p>
    </div>

    <div class="section" id="zend.log.formatters.xml"><div class="info"><h1 class="title">Formatting to XML</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Log_Formatter_Xml</span> formats log data into
            <acronym class="acronym">XML</acronym> strings. By default, it automatically logs all items in the event
            data array:
        </p>

        <pre class="programlisting brush: php">
$writer = new Zend_Log_Writer_Stream(&#039;php://output&#039;);
$formatter = new Zend_Log_Formatter_Xml();
$writer-&gt;setFormatter($formatter);

$logger = new Zend_Log();
$logger-&gt;addWriter($writer);

$logger-&gt;info(&#039;informational message&#039;);
</pre>


        <p class="para">
            The code above outputs the following <acronym class="acronym">XML</acronym> (space added for clarity):
        </p>

        <pre class="programlisting brush: xml">
&lt;logEntry&gt;
  &lt;timestamp&gt;2007-04-06T07:24:37-07:00&lt;/timestamp&gt;
  &lt;message&gt;informational message&lt;/message&gt;
  &lt;priority&gt;6&lt;/priority&gt;
  &lt;priorityName&gt;INFO&lt;/priorityName&gt;
&lt;/logEntry&gt;
</pre>


        <p class="para">
            It&#039;s possible to customize the root element as well as specify a mapping
            of <acronym class="acronym">XML</acronym> elements to the items in the event data array. The constructor
            of <span class="classname">Zend_Log_Formatter_Xml</span> accepts a string with the name of
            the root element as the first parameter and an associative array with
            the element mapping as the second parameter:
        </p>

        <pre class="programlisting brush: php">
$writer = new Zend_Log_Writer_Stream(&#039;php://output&#039;);
$formatter = new Zend_Log_Formatter_Xml(&#039;log&#039;,
                                        array(&#039;msg&#039; =&gt; &#039;message&#039;,
                                              &#039;level&#039; =&gt; &#039;priorityName&#039;)
                                       );
$writer-&gt;setFormatter($formatter);

$logger = new Zend_Log();
$logger-&gt;addWriter($writer);

$logger-&gt;info(&#039;informational message&#039;);
</pre>


        <p class="para">
            The code above changes the root element from its default of
            <span class="property">logEntry</span> to <span class="property">log</span>. It also maps the element
            <span class="property">msg</span> to the event data item <span class="property">message</span>. This
            results in the following output:
        </p>

        <pre class="programlisting brush: xml">
&lt;log&gt;
  &lt;msg&gt;informational message&lt;/msg&gt;
  &lt;level&gt;INFO&lt;/level&gt;
&lt;/log&gt;
</pre>

    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.log.writers.html">Writers</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.log.html">Zend_Log</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.log.filters.html">Filters</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.log.html">Zend_Log</a></li>
  <li><a href="zend.log.overview.html">Overview</a></li>
  <li><a href="zend.log.writers.html">Writers</a></li>
  <li class="active"><a href="zend.log.formatters.html">Formatters</a></li>
  <li><a href="zend.log.filters.html">Filters</a></li>
  <li><a href="zend.log.factory.html">Using the Factory to Create a Log</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>